package com.gpframework.module.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.manufacture.domain.GpWeightReductionRuleSetting;
import com.gpframework.module.manufacture.service.IGpWeightReductionRuleSettingService;
import com.gpframework.module.purchase.domain.GpPurchaseGramWeight;
import com.gpframework.module.purchase.domain.GpPurchaseIncomingInspect;
import com.gpframework.module.purchase.domain.GpPurchasingMakingDetails;
import com.gpframework.module.purchase.domain.req.GpPurchaseIncomingInspectUpdate;
import com.gpframework.module.purchase.domain.vo.GpPurchaseGramWeightVo;
import com.gpframework.module.purchase.mapper.GpPurchaseGramWeightMapper;
import com.gpframework.module.purchase.service.IGpPurchaseGramWeightService;
import com.gpframework.module.purchase.service.IGpPurchaseIncomingInspectService;
import com.gpframework.module.purchase.service.IGpPurchasingMakingDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpPurchaseGramWeight
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 采购克重Service业务层处理
 */
@Service
public class GpPurchaseGramWeightServiceImpl extends ServiceImpl<GpPurchaseGramWeightMapper, GpPurchaseGramWeight> implements IGpPurchaseGramWeightService {

    @Autowired
    private IGpPurchaseIncomingInspectService iGpPurchaseIncomingInspectService;
    @Autowired
    private IGpPurchasingMakingDetailsService iGpPurchasingMakingDetailsService;

    @Autowired
    private  IGpWeightReductionRuleSettingService iGpWeightReductionRuleSettingService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [lqw]
     * @Return List<GpPurchaseGramWeightVo>
     */
    @Override
    public List<GpPurchaseGramWeightVo> listVo(QueryWrapper<GpPurchaseGramWeight> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [id]
     * @Return GpPurchaseGramWeightVo
     */
    @Override
    public GpPurchaseGramWeightVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public boolean addWeightMeter(GpPurchaseIncomingInspectUpdate gpPurchaseIncomingInspectUpdate) {
        List<Long> ids = gpPurchaseIncomingInspectUpdate.getIds();
        GpPurchaseIncomingInspect gpPurchaseIncomingInspect;
        GpPurchasingMakingDetails gpPurchasingMakingDetails;
        List<GpPurchaseGramWeight> list = new ArrayList<>();
        for (Long id : ids) {
            gpPurchaseIncomingInspect = iGpPurchaseIncomingInspectService.getById(id);
            if (gpPurchaseIncomingInspect == null) {
                continue;
            }
            GpPurchaseGramWeight gpPurchaseGramWeight = new GpPurchaseGramWeight();
            BeanUtils.copyProperties(gpPurchaseIncomingInspect,gpPurchaseGramWeight);
            if (StringUtils.isNotBlank(gpPurchaseIncomingInspectUpdate.getCreateBy())) {
                gpPurchaseGramWeight.setCreateBy(gpPurchaseIncomingInspectUpdate.getCreateBy());
            }
            gpPurchaseGramWeight.setIncomingInspectId(gpPurchaseIncomingInspect.getId());
            gpPurchaseGramWeight.setCheckTime(new Date());
            // 获取做货明细中的克重
            gpPurchasingMakingDetails = iGpPurchasingMakingDetailsService.getById(gpPurchaseIncomingInspect.getPurchasingDetailsId());
            if (gpPurchasingMakingDetails != null) {
                gpPurchaseGramWeight.setStandardWeight(BigDecimal.valueOf(gpPurchasingMakingDetails.getFabricWeight()));
            }

            // 获取克重规则配置 标准克重
            GpWeightReductionRuleSetting one = iGpWeightReductionRuleSettingService.getOne(new LambdaQueryWrapper<GpWeightReductionRuleSetting>().eq(GpWeightReductionRuleSetting::getProductName, gpPurchaseIncomingInspect.getProductName()).eq(GpWeightReductionRuleSetting::getDelStatus, YesNoEnum.NO.getKey()));
            if (one!=null){
                gpPurchaseGramWeight.setStandardWeight(new BigDecimal(one.getStandardWeight()));
            }


            list.add(gpPurchaseGramWeight);
        }
        return this.saveBatch(list);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpPurchaseGramWeight gpPurchaseGramWeight = this.getById(id);
        if (gpPurchaseGramWeight == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpPurchaseGramWeight.setId(null);
            this.save(gpPurchaseGramWeight);
        }
        return true;
    }
}
